package com.joyplus.report.business.service;

import com.alibaba.fastjson.JSON;
import com.joyplus.report.business.dao.MdCampaignSupplementDao;
import com.joyplus.report.business.domain.MdCampaignSupplement;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.joyplus.report.common.service.BaseService;
import javax.annotation.Resource;


/**
 * Service
 *
 * @author jeff
 * @date 2018-05-31
 */
@Service
@Transactional
public class MdCampaignSupplementService extends
    BaseService<MdCampaignSupplementDao, MdCampaignSupplement> {

  @Resource
  private MdCampaignSupplementDao mdCampaignSupplementDao;

  @Autowired
  //@Qualifier(value = "primaryMongoTemplate")
  MongoTemplate mongoTemplate;

  @Transactional(readOnly = false)
  public void save(MdCampaignSupplement mdCampaignSupplement) {
    if (null == mdCampaignSupplement.getId()) {
      super.insert(mdCampaignSupplement);
    } else {
      super.update(mdCampaignSupplement);
    }
  }


  @Override
  public List<MdCampaignSupplement> findAll() {
    return mdCampaignSupplementDao.findAll();
  }

  /**
   * 獲取計劃投放的數據
   */
  public List<DBObject> getPlanCampaign(int id) {
    List<DBObject> pipeline = new ArrayList();
    List<String> multiply = new ArrayList();
    BasicDBObject groupAppend = new BasicDBObject("task_date", "$task_date")
        .append("campaign_id", "$campaign_id").append("count", "$count");

    BasicDBObject groupAppend1 = new BasicDBObject("task_date", "$_id.task_date")
        .append("campaign_id", "$_id.campaign_id");

    BasicDBObject sortAppend = new BasicDBObject("task_date", 1);

    BasicDBObject projectAppend = new BasicDBObject("task_date", "$_id.task_date")
        .append("campaign_id", "$_id.campaign_id")
        .append("pv", "$pv").append("_id", 0).append("count", "$count");
    BasicDBObject cond = new BasicDBObject("campaign_id", id + "");
    DBObject match = new BasicDBObject("$match", cond);
    DBObject groupFields1 = new BasicDBObject("_id", groupAppend);
    DBObject groupFields2 = new BasicDBObject("_id", groupAppend1);
    groupFields2.put("count", new BasicDBObject("$sum", "$_id.count"));
    groupFields2.put("pv", new BasicDBObject("$sum", "$pv"));
    multiply.add("$count");
    multiply.add("$freq");
    groupFields1.put("pv", new BasicDBObject("$sum", new BasicDBObject("$multiply", multiply)));
    DBObject group1 = new BasicDBObject("$group", groupFields1);
    DBObject group2 = new BasicDBObject("$group", groupFields2);
    DBObject project = new BasicDBObject("$project", projectAppend);
    DBObject sort = new BasicDBObject("$sort", sortAppend);
    pipeline.add(match);
    pipeline.add(group1);
    pipeline.add(group2);
    pipeline.add(project);
    pipeline.add(sort);
    System.out.println(JSON.toJSONString(pipeline));
    AggregationOutput pvUvStatistics = mongoTemplate
        .getCollection("optimization_task_statistics").aggregate(pipeline);
    Iterable<DBObject> results = pvUvStatistics.results();
    List<DBObject> collect = StreamSupport.stream(results.spliterator(), false)
        .collect(Collectors.toList());
    return collect;
  }


  /**
   * S2S獲取實際投放的數據
   */
  public List<DBObject> getS2SRealCampaign(int id) {
    List<DBObject> pipeline = new ArrayList();
    BasicDBObject groupAppend = new BasicDBObject("task_date", "$task_date")
        .append("campaign_id", "$campaign_id");
    BasicDBObject projectAppend = new BasicDBObject("task_date", "$_id.task_date")
        .append("campaign_id", "$_id.campaign_id")
        .append("pv", "$pv").append("uv", "$uv").append("_id", 0);
    BasicDBObject cond = new BasicDBObject("campaign_id", id + "");
    DBObject match = new BasicDBObject("$match", cond);
    DBObject groupFields1 = new BasicDBObject("_id", groupAppend);

    groupFields1.put("pv", new BasicDBObject("$sum", "$pv"));
    groupFields1.put("uv", new BasicDBObject("$sum", "$uv"));
    DBObject group1 = new BasicDBObject("$group", groupFields1);
    DBObject project = new BasicDBObject("$project", projectAppend);
    pipeline.add(match);
    pipeline.add(group1);
    pipeline.add(project);
    System.out.println(JSON.toJSONString(pipeline));
    AggregationOutput pvUvStatistics = mongoTemplate
        .getCollection("s2s_distribute_machine_statistics").aggregate(pipeline);
    Iterable<DBObject> results = pvUvStatistics.results();
    List<DBObject> collect = StreamSupport.stream(results.spliterator(), false)
        .collect(Collectors.toList());
    return collect;
  }


  /**
   * C2S獲取實際投放的數據
   */
  public List<DBObject> getC2SRealCampaign(int id) {
    List<DBObject> pipeline = new ArrayList();
    BasicDBObject groupAppend = new BasicDBObject("task_date", "$date")
        .append("campaign_id", "$campaign_id");
    BasicDBObject projectAppend = new BasicDBObject("task_date", "$_id.task_date")
        .append("campaign_id", "$_id.campaign_id")
        .append("total", "$total").append("_id", 0);
    BasicDBObject cond = new BasicDBObject("campaign_id", id + "");
    DBObject match = new BasicDBObject("$match", cond);
    DBObject groupFields1 = new BasicDBObject("_id", groupAppend);
    groupFields1.put("total", new BasicDBObject("$sum", "$pv"));
    DBObject group1 = new BasicDBObject("$group", groupFields1);
    DBObject project = new BasicDBObject("$project", projectAppend);
    pipeline.add(match);
    pipeline.add(group1);
    pipeline.add(project);
    System.out.println(JSON.toJSONString(pipeline));
    AggregationOutput pvUvStatistics = mongoTemplate
        .getCollection("c2s_distribution_machine_statistics").aggregate(pipeline);
    Iterable<DBObject> results = pvUvStatistics.results();
    List<DBObject> collect = StreamSupport.stream(results.spliterator(), false)
        .collect(Collectors.toList());
    return collect;
  }


}
